﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using SE560Multimedia.src;

namespace SE560Multimedia.pages
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        private SessionManager sessionManager;

        protected void Page_Load(object sender, EventArgs e)
        {
            sessionManager = new SessionManager(Session);

            if (!IsPostBack)
            {
                ContentRatingMedia.ItemId = Guid.NewGuid();
                ContentRatingMedia.DataSource = RatingValues;
                ContentRatingMedia.DataBind();

                LabelPlayingMediaName.Text = sessionManager.PlayingMediaName;
                LabelPlayingArtistName.Text = sessionManager.PlayingMediaArtistName;
                LabelPlayingAlbumName.Text = sessionManager.PlayingMediaAlbumName;
                WindowsMediaMainPlayer.VideoURL = getMediaFullPath();

                updateComments();
            }
        }

        private void updateComments()
        {
            ProjectDataSetTableAdapters.commentTableAdapter commentTableAdapter =
            new ProjectDataSetTableAdapters.commentTableAdapter();

            ProjectDataSet.commentDataTable commentResult =
                commentTableAdapter.GetDataByMediaID(sessionManager.PlayingMediaID);

            GridViewComments.DataSource = commentResult;
            GridViewComments.DataBind();
        }

        public String getRepositoryPath()
        {
            return ConfigurationSettings.AppSettings["MediaPath"];
        }

        protected void ButtonComment_Click(object sender, EventArgs e)
        {
            ProjectDataSetTableAdapters.commentTableAdapter commentTableAdapter =
                new ProjectDataSetTableAdapters.commentTableAdapter();

            commentTableAdapter.Insert(
                sessionManager.PlayingMediaID, 
                sessionManager.SessionLoggedUserId, 
                TextBoxMakeAComment.Text);

            updateComments();
        }

        protected void ButtonDownloadMedia_Click(object sender, EventArgs e)
        {
            DownloadFile(getMediaFullPath());
        }

        private string getMediaFullPath()
        {
            return getRepositoryPath() + sessionManager.PlayingMediaName;
        }

        public void DownloadFile(string filePath)
        {
            try
            {
                HttpContext.Current.Response.ContentType = 
                    "application/octet-stream";
                HttpContext.Current.Response.AddHeader(
                    "Content-Disposition", "attachment; filename=" + System.IO.Path.GetFileName(filePath));
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.WriteFile(filePath);
                HttpContext.Current.Response.End();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        protected void ContentRatingMedia_Rating(object sender, Spaanjaars.Toolkit.RateEventArgs e)
        {
            if( e.HasRated || 
                (long)( new ProjectDataSetTableAdapters.ratingTableAdapter().GetIsUserRatedMedia(
                sessionManager.SessionLoggedUserId,
                sessionManager.PlayingMediaID)) > 0)
            {
                e.Cancel = true;
            }
        }

        protected void ContentRatingMedia_Rated(object sender, Spaanjaars.Toolkit.RateEventArgs e)
        {
            ProjectDataSetTableAdapters.ratingTableAdapter ratingTblAdapter =
                    new ProjectDataSetTableAdapters.ratingTableAdapter();
            ratingTblAdapter.Insert(
                sessionManager.SessionLoggedUserId,
                sessionManager.PlayingMediaID, 
                (short)e.RateValue);

            int[] ratingValues = RatingValues;
            ContentRatingMedia.DataSource = ratingValues;
            ContentRatingMedia.DataBind();
        }

        public int[] RatingValues
        {
            get
            {
                ProjectDataSetTableAdapters.ratingTableAdapter ratingTblAdapter =
                    new ProjectDataSetTableAdapters.ratingTableAdapter();
                int mediaID = sessionManager.PlayingMediaID;

                int[] ratingValues = new int[5];

                for( int index = 1; index < 6; index++ )
                {
                    long ratingCount = 
                        (long)( ratingTblAdapter.GetRatingCountByMediaIDAndRating (mediaID, (short)index) );
                    ratingValues[index - 1] = (int)ratingCount;
                }

                return ratingValues;
            }
        }

        protected void ButtonAddToPlaylist_Click(object sender, EventArgs e)
        {
            ProjectDataSetTableAdapters.playlistitemTableAdapter plylistItemAdapter =
                new ProjectDataSetTableAdapters.playlistitemTableAdapter();
            
            String operationStatus;

            if( plylistItemAdapter.GetIsMediaAddedToPlaylist(
                 sessionManager.SessionLoggedPlaylistID, sessionManager.PlayingMediaID ) == 0 )
            {
                plylistItemAdapter.Insert(
                    sessionManager.PlayingMediaID, sessionManager.SessionLoggedPlaylistID);
                operationStatus = "Added to playlist";
            }else
            {
                operationStatus = "Can not add : Media already added to playlist";
            }
            LabelOperationStatus.Text = operationStatus;

        }
    }
}
